package com.ruoyi.report_jkdj.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_jkdj.domain.bo.DjczljlBaDetailBo;
import com.ruoyi.report_jkdj.domain.vo.DjczljlBaDetailVo;
import com.ruoyi.report_jkdj.domain.DjczljlBaDetail;
import com.ruoyi.report_jkdj.mapper.DjczljlBaDetailMapper;
import com.ruoyi.report_jkdj.service.IDjczljlBaDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 地基承载力试验检测记录详细Service业务层处理
 *
 * @author zyb
 * @date 2024-07-25
 */
@RequiredArgsConstructor
@Service
@DSTransactional
public class DjczljlBaDetailServiceImpl implements IDjczljlBaDetailService {

    private final DjczljlBaDetailMapper baseMapper;

    /**
     * 查询地基承载力试验检测记录详细
     */
    @Override
    public DjczljlBaDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询地基承载力试验检测记录详细列表
     */
    @Override
    public TableDataInfo<DjczljlBaDetailVo> queryPageList(DjczljlBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DjczljlBaDetail> lqw = buildQueryWrapper(bo);
        Page<DjczljlBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询地基承载力试验检测记录详细列表
     */
    @Override
    public List<DjczljlBaDetailVo> queryList(DjczljlBaDetailBo bo) {
        LambdaQueryWrapper<DjczljlBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DjczljlBaDetail> buildQueryWrapper(DjczljlBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DjczljlBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, DjczljlBaDetail::getExcelId, bo.getExcelId());
        lqw.eq(bo.getXuh() != null, DjczljlBaDetail::getXuh, bo.getXuh());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuanghwz()), DjczljlBaDetail::getZhuanghwz, bo.getZhuanghwz());
        lqw.eq(bo.getChuijs() != null, DjczljlBaDetail::getChuijs, bo.getChuijs());
        lqw.eq(bo.getCzl() != null, DjczljlBaDetail::getCzl, bo.getCzl());
        return lqw;
    }

    /**
     * 新增地基承载力试验检测记录详细
     */
    @Override
    public Boolean insertByBo(DjczljlBaDetailBo bo) {
        DjczljlBaDetail add = BeanUtil.toBean(bo, DjczljlBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改地基承载力试验检测记录详细
     */
    @Override
    public Boolean updateByBo(DjczljlBaDetailBo bo) {
        DjczljlBaDetail update = BeanUtil.toBean(bo, DjczljlBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DjczljlBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除地基承载力试验检测记录详细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
